define([], function() {
    var app = angular.module("App.home");
    app.controller("projects.detail.deleteCtrl", ["$scope", "$http", "curPrj", "showInfoModal", "showMsg", "$timeout", function($scope, $http, curPrj, showInfoModal, showMsg, $timeout) {


        $scope.curPrj = curPrj;
        $scope.monitorCollection = [];
        curPrj.getMonitorPointsDictionary().then(function(data) {
            var monitorType = "",
                location = "",
                tempCollection = {};
            data.forEach(function(item, index, array) {
                if (item.MonitorType !== monitorType) {
                    monitorType = item.MonitorType;
                    tempCollection = {
                        monitorType: item.MonitorType,
                        locations: []
                    };
                    $scope.monitorCollection.push(tempCollection);
                }
                tempCollection.locations.push(item.Location);
            });
        });


        $('select.ui.dropdown').dropdown();
        $('#reset').popup();
        $scope.reset = function() {
            $scope.activeMonitorType = null;
            $scope.activeLocation = null;
        };

        $scope.deleteData = function() {
            if (!$scope.activeDate || isNaN(Date.parse($scope.activeDate))) {
                showInfoModal({
                    title: '错误',
                    content: '时间是必选的,请填写要删除的时间,或者检查你的时间格式'
                });
            } else {
                var deleteMonitorType = $scope.activeMonitorType ? $scope.activeMonitorType.monitorType : "";
                var deleteLocation = $scope.activeLocation || "";
                var deleteDate = $scope.activeDate;
                showInfoModal({
                    title: '警告',
                    content: '即将删除 ' + deleteDate + '的' + deleteLocation + deleteMonitorType + '数据,数据一旦删除就不可恢复,确认删除吗?',
                    button: 2,
                    onConfirm: function() {
                        curPrj.deletePoints({
                            date: deleteDate,
                            location: deleteLocation,
                            monitorType: deleteMonitorType
                        }).then(function() {
                            //成功
                            $timeout(function() {
                                showMsg('删除成功', 'info');
                            }, 1000);
                        }, function() {
                            $timeout(function() {
                                showMsg('删除发生错误', 'alert');
                            }, 1000);
                        });
                    }
                });
            }
        };

        $scope.submit = function() {
            $scope.status = "uploading";
            curPrj.status = $scope.item.status ? 1 : 0;
            curPrj
                .postProperties()
                .then(function() {
                    $scope.status = "succeeded";
                }, function() {
                    $scope.status = "failed";
                });
        };
    }]);
});
